package com.zh.socket.websocket;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.socket.WebSocketSession;

import java.util.*;

/**
 * Class Name : ThmsWebSocketUserCache<br/>
 * Description: 利用缓存存储user对象(未完待续。。。，暂时采用内存存储)<br/>
 * Date : 2017年12月7日<br/>
 *
 * @author lds
 * @Version V1.0.0
 * @since JDK 1.7
 *
 */
public class ThmsWebSocketUserMap {

	/**
	 * userMap : 用户数据结构<br/>
	 * 
	 * @since JDK 1.7
	 *
	 */
	private static Map<String, Map<String, WebSocketSession>> userMap = new HashMap<String, Map<String, WebSocketSession>>();

	/**
	 * Description: 用户上线<br/>
	 * Date : 2017年12月7日<br/>
	 *
	 * @author lds
	 * @param user
	 * @since JDK 1.7
	 *
	 */
	public static void addUser(String key, String type, WebSocketSession userSession) {
		if (userMap.containsKey(type)) {
			Map<String, WebSocketSession> typeMap = userMap.get(type);
			if (null == typeMap) {
				typeMap = new HashMap<String, WebSocketSession>();
				typeMap.put(key, userSession);
				userMap.put(type, typeMap);
			} else {
				typeMap.put(key, userSession);
			}
		} else {
			Map<String, WebSocketSession> typeMap = new HashMap<String, WebSocketSession>();
			typeMap.put(key, userSession);
			userMap.put(type, typeMap);
		}
	}

	/**
	 * Description: 获取用户<br/>
	 * Date : 2017年12月7日<br/>
	 *
	 * @author lds
	 * @since JDK 1.7
	 *
	 */
	public static WebSocketSession getUserSession(String userType, String userKey) {
		if (userMap.containsKey(userType)) {
			WebSocketSession userSession = userMap.get(userType).get(userKey);
			return userSession;
		}
		return null;
	}

	/**
	 * Description: 用户下线<br/>
	 * Date : 2017年12月7日<br/>
	 *
	 * @author lds
	 * @since JDK 1.7
	 *
	 */
	public static void removeUser(String userType, String userKey) {
		if (userMap.containsKey(userType)) {
			userMap.get(userType).remove(userKey);
		}
	}

	public static List<WebSocketSession> getWebSocketSessions(String userType) {
		if (null == userMap) {
			return null;
		}
		List<WebSocketSession> sessions = new ArrayList<>();
		if (StringUtils.isBlank(userType)) {
			for (Map<String, WebSocketSession> map : userMap.values()) {
				if (null != map) {
					for (WebSocketSession user : map.values()) {
						if (null != user) {
							sessions.add(user);
						}
					}
				}
			}
		} else {
			Map<String, WebSocketSession> map = userMap.get(userType);
			if (null != map) {
				for (WebSocketSession user : map.values()) {
					if (null != user) {
						sessions.add(user);
					}
				}
			}
		}
		return sessions.size() > 0 ? sessions : null;
	}

	//获取同一个家庭的成员链接
	public static List<WebSocketSession> getWebSocketSessionsFamily(String userType,String userKey) {
		if (null == userMap) {
			return null;
		}
		List<WebSocketSession> sessions = new ArrayList<>();
		if (StringUtils.isNotBlank(userType)&&StringUtils.isNotBlank(userKey)) {
			Map<String, WebSocketSession> map = userMap.get(userType);
			if (null != map) {

				for (Map.Entry<String, WebSocketSession> entry : map.entrySet()) {
					String key = entry.getKey();
					WebSocketSession value = entry.getValue();
					if(key.contains("@")){
						String[] keys = key.split("@");
						if(Objects.equals(keys[0],userKey)){
							if(value!=null){
								sessions.add(value);
							}
						}
					}
				}
			}
		}
		return sessions.size() > 0 ? sessions : null;
	}

	public static List<WebSocketSession> getWebSocketSessions(String userType, String prefix) {
		if (null == userMap) {
			return null;
		}
		List<WebSocketSession> sessions = new ArrayList<>();
		if (StringUtils.isNotBlank(userType) && StringUtils.isNotBlank(prefix)) {
			Map<String,WebSocketSession> map = userMap.get(userType);
			if(null!=map) {
				for (Map.Entry<String,WebSocketSession> wss : map.entrySet()) {
					if(wss.getKey().startsWith(prefix)) {
						sessions.add(wss.getValue());
					}
				}
			}
			
		}
		return sessions.size() > 0 ? sessions :null;
	}

}
